﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Ionic.Zip;

namespace Helper
{
    public  class FileTools
    {
        /// <summary>
        /// Распаковать файлы из папки FolderPatch в папку TargetFolder
        /// </summary>
        /// <param name="FolderPatch">Исходная папка</param>
        /// <param name="TargetFolder">Целевая папка</param>
        public static void UnZipFolderFiles(string folderPatch, string targetFolder)
        {
            //Получаем файлы папки поочередно - копируем распаковываем 
            string[] fList = GetFileList(folderPatch,"*.zip");
            foreach (string s in fList)
            {
                Decompress(s, targetFolder );  
            }

        }

        public static void Decompress(string zipFile )
        {
            FileInfo fi = new FileInfo(zipFile);
            string unpackDirectory = fi.DirectoryName;
            if (fi.Extension.ToUpper().IndexOf("ZIP") > -1)
                DecompressZip(zipFile, unpackDirectory);
            else
                DecompressRar(zipFile, unpackDirectory);
        }

        public static void Decompress(string zipFile, string unpackDirectory)
        {
          
            FileInfo fi= new FileInfo (zipFile ) ;
            if(fi.Extension.ToUpper () .IndexOf("ZIP")>-1)
                  DecompressZip( zipFile, unpackDirectory);
            else
                DecompressRar (zipFile, unpackDirectory);
        }

        public static void DecompressZip(string zipFile, string unpackDirectory)
        {
            using (ZipFile zip1 = ZipFile.Read(zipFile))
            {
                foreach (ZipEntry e in zip1)
                {
                    e.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);
                }
            }
        }

        public static void DecompressRar(string rarFile, string unpackDirectory)
        {
            Chilkat.Rar rar = new Chilkat.Rar();

            bool success;

            success = rar.Open(rarFile);
            if (success != true)
            {
                throw new Exception("Bad arhive " + rarFile);           
            }

            success = rar.Unrar(unpackDirectory);
            if (success != true)
            {
                throw new Exception("Bad arhive " + rarFile);           
            }          
        }

        public static string[] GetFileList(string folderPatch, string like)
        {
            return Directory.GetFiles(folderPatch, like, SearchOption.TopDirectoryOnly);
        }

        /// <summary>
        /// Проверить наличие папки и создать если она отсутсвует
        /// </summary>
        /// <param name="folderPatch"></param>
        public static void СeckAndCreateFolder(string folderPatch)
        {
            string[] r = folderPatch.Split(@"\".ToCharArray()[0]);
            string folderSet=string.Empty  ;
            foreach (string s in r)
            { 
                folderSet =folderSet +(string.IsNullOrEmpty(folderSet)?"":@"\")+s;
                CreateFolder(folderSet);
            }           
        }
        private static void CreateFolder(string folderPatch)
        {
            if (Directory.Exists(folderPatch))
                return;

            Directory.CreateDirectory(folderPatch);
        }
        /// <summary>
        /// Проверить наличие папки и создать если она отсутсвует
        /// </summary>
        /// <param name="folderPatch"></param>
        public static bool СeckFileExists(string fileName)
        {
            
            return File.Exists(fileName);               
        }

        public static void RenameFile(string oldName,string newName)
        {
            File.Delete(newName);

            FileInfo fi = new FileInfo(oldName);
 
            if (fi.Exists)
            {
                fi.MoveTo(newName);
 
            }            
        }

        public static void DeleteFile(string fileName)
        {
            File.Delete(fileName);
 
        }

        public static void RemoveFile(string oldName, string newName){
            if (File.Exists(newName))
                File.Delete(newName);

            File.Copy (oldName ,newName );
            File.Delete (oldName );
        }


        public static void RemoveToFolder(string fileName)
        {
            File.Delete(fileName);

        }
        public static bool СeckFolderExists(string folderName)
        {
            return Directory.Exists(folderName);
        }

       

        public static string[] GetFolderFiles(string path,string pattern)
        {
            return Directory.GetFiles (path,pattern);
        }        
    }
}
